package org.shock.ss.sms.action.app;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.shock.ss.sms.action.BaseAction;
import org.shock.ss.sms.bean.app.CustomerInfo;
import org.shock.ss.sms.bean.app.InRecords;
import org.shock.ss.sms.bean.app.OutRecords;
import org.shock.ss.sms.commons.annotation.RightAnno;
import org.shock.ss.sms.commons.dao.DaoFactory;
import org.shock.ss.sms.commons.db.DbTools;
import org.shock.ss.sms.commons.db.TsDbTools;
import org.shock.ss.sms.commons.utils.Util;
import org.shock.ss.sms.dao.app.impl.InRecordsDaoImpl;
import org.shock.ss.sms.dao.app.impl.OutRecordsDaoImpl;
import org.shock.ss.sms.service.app.InRecordsService;
import org.shock.ss.sms.service.app.OutRecordsService;
import org.shock.ss.sms.service.app.impl.InRecordsServiceImpl;
import org.shock.ss.sms.service.app.impl.OutRecordsServiceImpl;

import com.alibaba.fastjson.JSON;

public class OutRecordsAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private OutRecordsService os = new OutRecordsServiceImpl((OutRecordsDaoImpl) DaoFactory.getDao("OutRecordsDao"));
	private InRecordsService cs = new InRecordsServiceImpl((InRecordsDaoImpl) DaoFactory.getDao("InRecordsDao"));
	private List outRecordsList;
	private List list;
	private OutRecords outRecords = new OutRecords();
	private InRecords inRecords = new InRecords();
	private String customerinfo;
	private String ids;

	public String outRecordsAdmin() throws Exception {
		return SUCCESS;
	}

	public String outRecords() throws Exception {
		return SUCCESS;
	}

	/**
	 * 显示某产品号下所有货品包，用与出库验证
	 * 
	 * @return
	 * @throws Exception
	 */
	public String showAllCargo() throws Exception {
		String sql = "select a.id,a.cargoname,a.productcode,a.ordercode,concat(a.cargoid,b.plu) as cargoplu from ckgl_in_records a,ckgl_cargoplu b where a.cargoid=b.cargoid and a.productcode="
				+ outRecords.getProductcode();
		list = os.listAll(sql);
		jsonString = JSON.toJSONString(list);
		System.out.println(jsonString);
		return SUCCESS;
	}

	/**
	 * 显示尚未出库的产品列表，用于出库
	 * 
	 * @return
	 * @throws Exception
	 */
	public String showNoOutRecords() throws Exception {
		String sql = "";
		String sqlCount = "";
		if (!Util.isEmpty(inRecords.getProductcode())) {
			sqladd.append(" and productcode = '" + inRecords.getProductcode() + "'");
		}
		if (!Util.isEmpty(inRecords.getOrdercode())) {
			sqladd.append(" and ordercode = '" + inRecords.getOrdercode() + "'");
		}
		if (!Util.isEmpty(customerinfo)) {
			sqladd.append(" and customerid in(select id from " + CustomerInfo.getTable() + " where name like '%"
					+ customerinfo + "%' or phone like '%" + customerinfo + "%')");
		}
		sqladd.append(" and isck=0");
		sql = "select isck,productcode,ordercode,customerid,CONCAT(c.name,c.phone,c.province,c.city) as customername,sum(cargonum) as cargonum,max(intime) as intime from "
				+ InRecords.getTable()
				+ " a,"
				+ CustomerInfo.getTable()
				+ " c where a.customerid=c.id"
				+ sqladd
				+ " group by productcode,ordercode,customerid,c.name";
		System.out.println(sql);
		sqlCount = "select count(distinct productcode) from " + InRecords.getTable() + " where 1=1" + sqladd;
		if (pageno == 1) {
			pageno = (start / rownum) + 1;
		}
		pager = cs.list(dir, "a." + sort, pageno, rownum, sqlCount, sql);
		totalCount = pager.getTotalCount();
		outRecordsList = pager.getList();
		jsonString = JSON.toJSONString(pager);
		System.out.println(jsonString);
		return SUCCESS;
	}

	public String listOutRecords() throws Exception {
		String sql = "";
		String sqlCount = "";
		if (!Util.isEmpty(outRecords.getProductcode())) {
			sqladd.append(" and productcode = '" + outRecords.getProductcode() + "'");
		}
		if (!Util.isEmpty(outRecords.getOutcode())) {
			sqladd.append(" and outcode = '" + outRecords.getOutcode() + "'");
		}
		if ("distinct".equals(action)) {
			sql = "select productcode,ordercode,customerid,CONCAT(c.name,c.phone,c.province,c.city) as customername,sum(cargonum) as cargonum,max(intime) as intime from "
					+ OutRecords.getTable()
					+ " a,"
					+ CustomerInfo.getTable()
					+ " c where a.customerid=c.id"
					+ sqladd
					+ " group by productcode,ordercode,customerid,c.name";
			sqlCount = "select count(*) from " + OutRecords.getTable() + " where 1=1" + sqladd;
		} else {
			sql = "select * from " + OutRecords.getTable() + " a where 1=1" + sqladd;
			sqlCount = "select count(*) from " + OutRecords.getTable() + " where 1=1" + sqladd;
		}
		if (pageno == 1) {
			pageno = (start / rownum) + 1;
		}
		pager = os.list(dir, "a." + sort, pageno, rownum, sqlCount, sql);
		totalCount = pager.getTotalCount();
		outRecordsList = pager.getList();
		jsonString = JSON.toJSONString(pager);
		System.out.println(jsonString);
		return SUCCESS;
	}

	public String viewOutRecords() throws Exception {
		String sql = "select * from " + outRecords.getTable() + " where productcode='" + outRecords.getProductcode()
				+ "'";
		System.out.println(sql);
		Map map = os.view(sql);
		jsonString = JSON.toJSONString(map);
		System.out.println(jsonString);
		return SUCCESS;
	}

	@RightAnno(rightValue = "delOutRecords", rightNote = "删除出库单")
	public String delOutRecords() throws Exception {
		String outtime = DbTools.getString("select outtime from " + outRecords.getTable() + " where productcode ='"
				+ ids + "'");
		if (!outtime.equals(new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()))
				&& getUserInfor().getIsadmin() != 1) {
			throw new RuntimeException("只能删除当天的出库单！");
		}
		os.del(ids);
		success = true;
		message = "删除成功!";
		jsonString = "{success:" + success + ",message:'" + message + "'}";
		return SUCCESS;
	}

	@RightAnno(rightValue = "saveOutRecords", rightNote = "保存出库单")
	public String saveOutRecords() throws Exception {
		if (null == outRecords.getId()) {
			return addOutRecords();
		} else {
			return updateOutRecords();
		}
	}

	public String updateOutRecords() throws Exception {
		outRecords.setModifier(getUserInfor().getYgzh());
		outRecords.setLastmodifytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
		os.update(outRecords);
		message = "更新成功!";
		return SUCCESS;
	}

	public String addOutRecords() throws Exception {
		outRecords.setOpzh(getUserInfor().getYgzh());
		outRecords.setLastmodifytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
		os.add(outRecords);
		message = "添加成功!";
		return SUCCESS;
	}

	public List getOutRecordsList() {
		return outRecordsList;
	}

	public void setOutRecordsList(List outRecordsList) {
		this.outRecordsList = outRecordsList;
	}

	public OutRecords getOutRecords() {
		return outRecords;
	}

	public void setOutRecords(OutRecords outRecords) {
		this.outRecords = outRecords;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public InRecords getInRecords() {
		return inRecords;
	}

	public void setInRecords(InRecords inRecords) {
		this.inRecords = inRecords;
	}

	public String getCustomerinfo() {
		return customerinfo;
	}

	public void setCustomerinfo(String customerinfo) {
		this.customerinfo = customerinfo;
	}

}
